package com.alks.function.service.impl.oaImpl;

import com.alibaba.fastjson2.JSON;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.MinIoUtil;
import com.alks.common.utils.stringUtils.ZStringUtils;
import com.alks.entity.data.entity.oa.OaStyle;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.oa.OaStyleReq;
import com.alks.function.mapper.oa.OaStyleMapper;
import com.alks.function.service.oa.OaStyleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author:hxl
 * @create: 2024-09-10 14:05
 * @Description:
 */
@Service
public class OaStyleServiceImpl implements OaStyleService {

    @Autowired
    private OaStyleMapper oaStyleMapper;

    @Override
    @AutoPageAop
    public ResponseInfo getOaStyle(OaStyleReq oaStyleReq) {
        HashMap<String, Object> map = new HashMap<>();
        SysUserTokenDTO dto = UserIdThread.get();
        List<OaStyle> oaStyles = null;
        List<String> names = new ArrayList<>();
        if ("9".equals(oaStyleReq.getCreateChoose())) {
            if (ZStringUtils.isEmpty(oaStyleReq.getCreateUser())) return ResponseInfo.error("请输入创建人");
            names.add(oaStyleReq.getCreateUser());
            oaStyles = oaStyleMapper.getOaStyle(oaStyleReq, names);
        } else {
            //先查寻部门 根据parent_level 查询本部门和本分部
            String deptId = dto.getDeptId();
            //查询本部门的所有人
            Integer deptLevel = oaStyleMapper.getDptLevel(deptId);
            Integer parentLevel = oaStyleMapper.getParentLevel(deptId);
            List<String> currentDepts = oaStyleMapper.getCurrentDepts(parentLevel);
            List<String> currentName = oaStyleMapper.getCurrentName(currentDepts);
            List<String> childDept = oaStyleMapper.getChildDept(deptLevel);
            List<String> childNames = ZStringUtils.isEmpty(childDept) ? null : oaStyleMapper.getCurrentName(childDept);
            //查询下属
            List<String> upperName = oaStyleMapper.getUpperName(parentLevel);
            //查询上级
            switch (oaStyleReq.getCreateChoose()) {
                case "1":
                    oaStyles = oaStyleMapper.getOaStyle(oaStyleReq, names);
                    break;
                case "4":
                case "2":
                case "5":
                case "3":
                    oaStyles = oaStyleMapper.getOaStyle(oaStyleReq, currentName);
                    break;
                case "6":
                    names.add(dto.getUserName());
                    oaStyles = oaStyleMapper.getOaStyle(oaStyleReq, names);
                    break;
                case "7":
                    oaStyles = oaStyleMapper.getOaStyle(oaStyleReq, upperName);
                    break;
                case "8":
                    if (ZStringUtils.isEmpty(childNames)) {
                        map.put("list", null);
                        map.put("total", 0);
                        return ResponseInfo.ok(map);
                    }
                    oaStyles = oaStyleMapper.getOaStyle(oaStyleReq, childNames);
                    break;
            }
        }
        map.put("list", oaStyles);
        map.put("total", new PageInfo<>(oaStyles).getTotal());
        return ResponseInfo.ok(map);
    }

    @Autowired
    MinIoUtil minIoUtil;

    @Override
    @Transactional
    public ResponseInfo createOaStyle(OaStyle oaStyle) {
        oaStyle.setCompanyId(UserIdThread.get().getCompanyId());
        MultipartFile[] files = oaStyle.getPhotos();
        List<String> list = new ArrayList<>();
        extracted(oaStyle, files, list);
        oaStyleMapper.insertIntoOaStyle(oaStyle);
        return ResponseInfo.ok();
    }

    private void extracted(OaStyle oaStyle, MultipartFile[] files, List<String> list) {
        if (files != null) {
            for (MultipartFile file : files) {
                String photoPath = "/photo/profile/style/" + file.getOriginalFilename();
                minIoUtil.upload(file, "photo", photoPath);
                list.add("http://192.168.1.220:9000/photo" + photoPath);
            }
        }
        String string = JSON.toJSONString(list);
        oaStyle.setPhotoPath(string);
    }

    @Override
    @Transactional
    public ResponseInfo updateOaStyle(OaStyle oaStyle) {
        MultipartFile[] files = oaStyle.getPhotos();
        String path = oaStyle.getPhotoPath();
        List list = JSON.parseObject(path, List.class);
        extracted(oaStyle, files, list);
        oaStyleMapper.updateOaStyle(oaStyle);
        return ResponseInfo.ok();
    }

    @Override
    @Transactional
    public ResponseInfo deleteOaStyle(OaStyle oaStyle) {
        oaStyleMapper.deleteOaStyle(oaStyle);
        return ResponseInfo.ok();
    }
}
